using System;
using System.Numerics;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Clamp_RT : RT_Node
    {
        public RT_SingleValuePort Value_RT;

        public RT_SingleValuePort Min_RT;

        public RT_SingleValuePort Max_RT;

        public RT_SingleValuePort Result_RT;

        public Node_Clamp_RT(Node_Clamp sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            Value_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Value, this);

            Min_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Min, this);

            Max_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Max, this);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;
        }

        public AValue GetResult(DAG_Executor executor)
        {
            Result_RT.SourceValue.Reset();

            var valueType = Result_RT.SourceValue.ValueType;
            if (valueType == AValue.EValueType.Integer)
            {
                int value = Value_RT.GetValue(executor).ToInteger().Item2;
                int min = Min_RT.GetValue(executor).ToInteger().Item2;
                int max = Max_RT.GetValue(executor).ToInteger().Item2;

                int result = Math.Clamp(value, min, max);
                Result_RT.SourceValue.SetInteger(result);
            }
            else if (valueType == AValue.EValueType.Scalar)
            {
                float value = Value_RT.GetValue(executor).ToScalar().Item2;
                float min = Min_RT.GetValue(executor).ToScalar().Item2;
                float max = Max_RT.GetValue(executor).ToScalar().Item2;

                float result = Math.Clamp(value, min, max);
                Result_RT.SourceValue.SetScalar(result);
            }
            else if (valueType == AValue.EValueType.Vector2)
            {
                Vector2 value = Value_RT.GetValue(executor).ToVector2().Item2;
                Vector2 min = Min_RT.GetValue(executor).ToVector2().Item2;
                Vector2 max = Max_RT.GetValue(executor).ToVector2().Item2;

                Vector2 result = Vector2.Clamp(value, min, max);
                Result_RT.SourceValue.SetVector2(result);
            }
            else if (valueType == AValue.EValueType.Vector3)
            {
                Vector3 value = Value_RT.GetValue(executor).ToVector3().Item2;
                Vector3 min = Min_RT.GetValue(executor).ToVector3().Item2;
                Vector3 max = Max_RT.GetValue(executor).ToVector3().Item2;

                Vector3 result = Vector3.Clamp(value, min, max);
                Result_RT.SourceValue.SetVector3(result);
            }
            return Result_RT.SourceValue;
        }


    }
}